<?php 

	function get_sidebars()
	{
		global $wpdb;
		$display_sidebars_query = 'SELECT * FROM `'.$wpdb->prefix.'lazar_sidebars` ORDER BY ID';
		$display_sidebars = $wpdb->get_results($display_sidebars_query);
		return $display_sidebars;
	}


	function insert_new_sidebar($id, $sidebar_name, $sidebar_description)  //  USED FOR ADDING A SIDEBAR
	{    
		global $wpdb;
		$insert_query = "INSERT INTO `".$wpdb->prefix."lazar_sidebars` (`id`, `name`, `description`) VALUES ('".$id."', '".$sidebar_name."','".$sidebar_description."');";
		$insert = $wpdb->get_results($insert_query);	
	}

	if ( isset($_POST['add_new_sidebar']) && $_POST['add_new_sidebar'] == 'true')
	{
		add_new_sidebar();
	}
	
	function add_new_sidebar()
	{
		$sidebar_name = '';
		if ( isset ($_POST['name']) )
			$sidebar_name = $_POST['name'];

		$sidebar_description = '';
		if ( isset ($_POST['description']) )
			$sidebar_description = $_POST['description'];
			
		if ( $sidebar_name != '' )
			insert_new_sidebar (NULL, $sidebar_name, $sidebar_description);
		
		
		$refer = $_SERVER['HTTP_REFERER'];
		
		echo "<script language=\"JavaScript\">
        <!--
        document.location.href  = \"".$refer."\";
        -->
        </script>";
	}

	function create_wordpress_admin_sidebars_submenu()
	{
		add_submenu_page( 'my-top-level-handle', 'Sidebars Options', 'Sidebars Options', 'manage_options', 'my-submenu-sidebars-handle', 'sidebars_admin_page');
		add_action('admin_init', 'register_sidebars_settings');
	} 

	function register_sidebars_settings()
	{
		register_setting('sidebars_options', 'name');
		register_setting('sidebars_options', 'description');
		register_setting('sidebars_options', 'database_action');
	}
	
	function sidebars_admin_page() {
	
	//CREATE FUNCTION
	function create_tables($table_name)
	{
		global $wpdb;
		
		if ($table_name == $wpdb->prefix.'lazar_sidebars')
		{
			$create_query = 'CREATE TABLE `'.$wpdb->prefix.'lazar_sidebars` (
								`id` INT NOT NULL AUTO_INCREMENT ,
								`name` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
								`description` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
								PRIMARY KEY (`id`)
							) ENGINE = MYISAM ;
								';
			$create = $wpdb->get_results($create_query);
		}
	}
	
	//CHECK FUNCTION
	function check_if_db_exists($database)
	{
		global $wpdb;
		if($wpdb->get_var("SHOW TABLES LIKE '".$database."'") != $database)
			return '';
		
		else 
			return 'table_exists';
	}


	global $wpdb;
	if ( check_if_db_exists($wpdb->prefix.'lazar_sidebars') == '') create_tables($wpdb->prefix.'lazar_sidebars');


	
	function delete_entry($id)
	{
		global $wpdb;
		$delete_query = 'DELETE FROM `'.$wpdb->prefix.'lazar_sidebars` WHERE id='.$id.' LIMIT 1';
		$wpdb->get_results($delete_query);	
		
		$refer = $_SERVER['HTTP_REFERER'];
		
		echo "<script language=\"JavaScript\">
        <!--
        document.location.href  = \"".$refer."\";
        -->
        </script>";
	}
	
	if(isset($_GET['delete']))
		if($_GET['delete'] != '')
			delete_entry($_GET['delete']); // IF CALLED DELETES A SIDEBAR BY ID 
?>	
	<script type="text/javascript" language="javascript">
		/* function for confirming you want to delete */
		function confirmAction()
		{
			return confirm("Are you sure you want to delete this sidebar?")
		}
		
		jQuery(document).ready(function() {
			jQuery('.header').click(function() {
				jQuery(this).next('.hidden_content').stop(true, true).toggle('fast');
			});
		});
		
		jQuery(document).ready(function() {
			jQuery("#sidebar_tabs").tabs();
			jQuery(".transform").jqTransform();
		});
	</script>
    
<div class="wrap">  
    <div id="icon-options-general" class="icon32"><br /></div>
    <h2>Sidebar Options</h2>  
    
     <?php
	$get_admin_warnings = admin_warnings();
	if ( $get_admin_warnings != '' )
	{
		echo '<div id="admin_warnings">';
			echo '<div id="admin_warning_icon"></div>';
			echo $get_admin_warnings;
		echo '</div>';
	}
	?>
    
<div id="sidebar_tabs" class="admin_tabs">
    <ul>
        <li>
            <a href="#tab_current_sidebars">
                Current Sidebars
            </a>
        </li>
        <li>
            <a href="#tab_add_sidebar">
                Add Sidebar
            </a>
        </li>
    </ul>
    
    
    
    <div id="tab_current_sidebars">
    <?php
	$display_sidebars = get_sidebars();
		
		if (count($display_sidebars) != 0)
		{
			echo' 
			<table id="sort_slider" class="display_elements" cellspacing="0">
				<thead>
					<tr>
						<th class="title">Name</th>
						<th>Description</th>
						<th>Actions</th>
					</tr>
				</thead>';
				
			echo '<tbody>';
				$i = 0; // to add class to TR 
				global $wpdb;
				foreach ( $display_sidebars as $sidebar )
				{
					if ( $i == 0 ) echo '<tr class="alternate" valign="top">';
					else echo '<tr>';
						echo '<td>';
							echo $sidebar->name;
						echo '</td>';
						echo '<td>';
							echo $sidebar->description;
						echo '</td>';
						echo '<td valign="center" align="center">';
							echo '<a class="delete" onClick="return confirmAction()" href="?page=my-submenu-sidebars-handle&amp;delete='.$sidebar->id.'">Delete</a>';
						echo '</td>';
					echo '</tr>';
					
					$i++;
					if ( $i == 2 ) $i = 0;
				
				}  // END  foreach ( $display_sidebars as $sidebar )
			echo '</tbody>';
		 
			echo '
				<tfoot>
					<tr>
						<th>Name</th>
						<th>Description</th>
						<th>Actions</th>
					</tr>
				</tfoot>
			</table>
			';
		} // END if (count($display_portfolios) != 0)
		
		else 
		{
			echo '<div id="admin_warnings">';
				echo 'There are no custom sidebars defined';
			echo '</div>';
		}
	?>
    </div> <!-- #tab_current_portfolios" -->
    
    
    <div id="tab_add_sidebar">
    	<form method="post" action="#tab_add_sidebar" class="transform">
        	<input type="hidden" name="add_new_sidebar" value="true" />
        
            <div class="settings_wrapper">
                <div class="header">
                    Add new Sidebar
                </div>
                <div class="row_data">
        
                    <div class="row clearfix">
                        <label>Sidebar name</label>
                        <input type="text" name="name" class="input_box" value="" />
                    </div>
                    <div class="row clearfix alternate">
                        <label>Sidebar description</label>
                        <input type="text" name="description" class="input_box" value="" />
                    </div>
                    <div class="row clearfix">
                        <input name="Submit" type="submit" class="add_content_button" value="<?php esc_attr_e('Add Sidebar'); ?>" />
                    </div>

                </div>
                
                <div class="footer">
                    Add new Sidebar
                </div>
            </div>
	
		</form>
   	</div> <!-- #tab_add_portfolio -->
    
    
</div> <!-- end #portfolio_tabs -->
    
</div> <!-- END  .wrap  -->  

<?php
	} // END portfolio_admin_page()
?>